package com.cup.bddm.api.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cup.bddm.api.entity.MerchantEntity;
import com.cup.bddm.api.mapper.MerchantEntityMapper;
import com.cup.bddm.api.req.MerchantVo;
import com.cup.bddm.util.StrUtil;

@Service("api-merchant-service")
public class MerchantService {
	@Autowired
	MerchantEntityMapper merchantEntityMapper;

	public List<MerchantEntity> findByVo(MerchantVo vo) {
		Map<String, Object> pms = new HashMap<>();
		pms.put("cup_branch_ins_id_cd", vo.getCup_branch_ins_id_cd());
		pms.put("mchnt_cd", vo.getMchnt_cd());
		pms.put("store_nm", vo.getStore_nm());
		return merchantEntityMapper.pageByPms(pms, vo.pager());
	}

	public int add(MerchantVo vo) {
		MerchantEntity entity = new MerchantEntity();
		entity.setCup_branch_ins_id_cd(vo.getCup_branch_ins_id_cd());
		entity.setCup_branch_nm(vo.getCup_branch_nm());
		entity.setStore_nm(vo.getStore_nm());
		entity.setStore_nm_index(StrUtil.indexEncode(vo.getStore_nm()));
		entity.setMchnt_cd(vo.getMchnt_cd());
		entity.setStreet_cd(vo.getStreet_cd());
		entity.setStore_addr(vo.getStore_addr());
		entity.setTrans_at_per_bill(vo.getTrans_at_per_bill());
		entity.setMchnt_tp(vo.getMchnt_tp());
		entity.setContact(vo.getContact());
		entity.setTel(vo.getTel());
		entity.setNfc_flag(vo.getNfc_flag());
		entity.setPwd_sign_flag(vo.getPwd_sign_flag());
		entity.setQrc_flag(vo.getQrc_flag());
		entity.setScan_tp(vo.getScan_tp());
		entity.setCashier_train_flag(vo.getCashier_train_flag());
		entity.setLogo_post_flag(vo.getLogo_post_flag());
		entity.setLogo_num(vo.getLogo_num());
		entity.setTerm_id(vo.getTerm_id());
		if (vo.getTerm_id() != null && vo.getTerm_id().length() > 8) {
			entity.setTerm_id_2(null);
		} else {
			entity.setTerm_id_2(vo.getTerm_id());
		}
		entity.setCompetitor(vo.getCompetitor());
		entity.setCompetitor_tp(vo.getCompetitor_tp());
		entity.setCompetitor_coupon(vo.getCompetitor_coupon());
		entity.setIs_del(0);
		entity.setCreate_time(new Date());
		entity.setUpdate_time(new Date());
//		entity.setDel_time(vo.getDel_time());
		entity.setStore_id(StrUtil.getMerchantStoreId(entity));
		entity.setRow_key(StrUtil.getMerchantRowKey(entity));
		return merchantEntityMapper.save(entity);
	}

	public int modify(MerchantEntity entity, MerchantVo vo) {
        entity.setCup_branch_ins_id_cd(vo.getCup_branch_ins_id_cd());
        entity.setCup_branch_nm(vo.getCup_branch_nm());
        entity.setStore_nm(vo.getStore_nm());
        entity.setStore_nm_index(StrUtil.indexEncode(vo.getStore_nm()));
        entity.setMchnt_cd(vo.getMchnt_cd());
        entity.setStreet_cd(vo.getStreet_cd());
        entity.setStore_addr(vo.getStore_addr());
        entity.setTrans_at_per_bill(vo.getTrans_at_per_bill());
        entity.setMchnt_tp(vo.getMchnt_tp());
        entity.setContact(vo.getContact());
        entity.setTel(vo.getTel());
        entity.setNfc_flag(vo.getNfc_flag());
        entity.setPwd_sign_flag(vo.getPwd_sign_flag());
        entity.setQrc_flag(vo.getQrc_flag());
        entity.setScan_tp(vo.getScan_tp());
        entity.setCashier_train_flag(vo.getCashier_train_flag());
        entity.setLogo_post_flag(vo.getLogo_post_flag());
        entity.setLogo_num(vo.getLogo_num());
            entity.setTerm_id(vo.getTerm_id());
            if (vo.getTerm_id().length() > 8) {
                entity.setTerm_id_2(null);
            } else {
                entity.setTerm_id_2(vo.getTerm_id());
            }
        entity.setCompetitor(vo.getCompetitor());
        entity.setCompetitor_tp(vo.getCompetitor_tp());
        entity.setCompetitor_coupon(vo.getCompetitor_coupon());
        entity.setUpdate_time(new Date());
		entity.setStore_id(StrUtil.getMerchantStoreId(entity));
		entity.setRow_key(StrUtil.getMerchantRowKey(entity));
		return merchantEntityMapper.updateById(entity);
	}

	public int setIsDel(MerchantEntity entity) {
		entity.setIs_del(1);
		entity.setDel_time(new Date());
		return merchantEntityMapper.updateById(entity);
	}

	public int delete(MerchantEntity entity) {
		return merchantEntityMapper.deleteById(entity.getId());
	}

	public MerchantEntity oneByMchntCdAndTermId(MerchantVo vo, boolean unDel) {
		Map<String, Object> pms = new HashMap<>();
		pms.put("cup_branch_ins_id_cd", vo.getCup_branch_ins_id_cd());
		pms.put("mchnt_cd", vo.getMchnt_cd());
		pms.put("term_id", vo.getTerm_id());
		if (unDel) {
			pms.put("is_del", "0");
		}
		return merchantEntityMapper.oneByMchntCdAndTermId(pms);
	}

	public MerchantEntity oneByStreetCd(String street_cd, boolean unDel) {
		Map<String, Object> pms = new HashMap<>();
		pms.put("street_cd", street_cd);
		if (unDel) {
			pms.put("is_del", "0");
		}
		return merchantEntityMapper.oneByStreetCd(pms);
	}

	public MerchantEntity oneById(MerchantVo vo, boolean unDel) {
		Map<String, Object> pms = new HashMap<>();
		pms.put("cup_branch_ins_id_cd", vo.getCup_branch_ins_id_cd());
		pms.put("id", vo.getId());
		if (unDel) {
			pms.put("is_del", "0");
		}
		return merchantEntityMapper.oneById(pms);
	}

	public List<MerchantEntity> matchByNm(String cup_branch_ins_id_cd, String nm) {
		Map<String, Object> pms = new HashMap<>();
		pms.put("cup_branch_ins_id_cd", cup_branch_ins_id_cd);
		pms.put("nm", nm);
		return merchantEntityMapper.matchByNm(pms);
	}

}
